#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time:2020/10/31 5:55 下午
# @Author:boyizhang
from typing import Optional

from app import schemals, models
from sqlalchemy.orm import Session
from app.models import User, UserType


# def get_db():
#     db = Session()
#     try:
#         yield db
#     finally:
#         db.close()
from enum import Enum,unique
@unique
class Status(Enum):
    todo = "todo"
    doing = "进行中"
    done = "已完成"
    block = "阻塞"
    # def __repr__(self):
    #     return
    active = "active"

class ProjectDao(object):
    def create_project(self, db: Session, project: schemals.CreateProject):
        # 添加

        db_project = models.Project(**project.dict(),status=Status.todo.value)
        db.add(db_project)
        db.commit()

        return db_project
    def update_project(self, db: Session, project: schemals.Project):
        db_project = db.query(models.Project) \
            .filter(models.Project.project_id == project.project_id) \
            .update(schemals.Project)

        db.commit()
        return db_project

    def get_all_project(self, db: Session,  pageNum: int = 0,
                        pageSize: int = 10):
        Project = models.Project
        # if project_name and status:
        #     db_projects = db.query(Project).filter(Project.project_name == project_name) \
        #         .filter(Project.status == status).offset(pageNum).limit(pageSize).all()
        # else:
        db_projects = db.query(Project).offset(pageNum).limit(pageSize).all()
        return db_projects

    def get_project_by_id(self, db: Session, project_id: int):
        db_project = db.query(models.Project).filter(models.Project.project_id == project_id).first()
        return db_project


class UserDao(object):
    def create_user(self, db: Session, user: schemals.CreateUser):
        db_user = User(**user.dict())
        db.add(db_user)
        db.commit()
        return db_user

    def update_user(self, db: Session, user: schemals.User):
        db_user = db.query(User).filter(User.user_id == user.user_id).update(user)
        db.commit()

    def get_all_users(self, db: Session, pageNum: int = 0, pageSize: int = 10):
        db_users = db.query(User).offset(pageNum).limit(pageSize).all()
        return db_users

    def get_user_by_id(self, db: Session, user_id: int):
        db_user = db.query(User).filter(User.user_id == user_id).first()
        return db_user

    def get_user_by_type_id(self, db: Session, type_id: int, pageNum: int = 0, pageSize: int = 10):
        # db_users = db.query.join(User, UserType.type_id == User.type_id).filter(User.type_id == type_id).all()
        db_users = db.query(User).filter(User.type_id == type_id).offset(pageNum).limit(pageSize).all()
        return db_users

    def cu_usertype(self, db: Session, usertype: schemals.UserType):
        if usertype.type_id:
            # 更新
            db_usertype = db.query(UserType).filter(UserType.type_id == usertype.type_id).update(usertype)
            db.commit()
        else:
            db_usertype = UserType(**usertype.dict())
            db.add(db_usertype)
            db.commit()
        return db_usertype

    def get_usertype_by_id(self, db: Session, type_id: int):
        return db.query(UserType).filter(UserType.type_id == type_id).first()
